Musiker.modPlayer = {
	name			: 	"Player",
	currentPlaylist	: 	null
}

Musiker.modPlayer.Initialize = function () {
	// STATUS FRAME CONTENT
	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlayerStatus';
		Musiker.modPlayer.StatusPanel = aFrame;
	Musiker.frames.HeaderPanel.appendChild(aFrame);

	var aFrame = document.createElement('a');
		aFrame.className = 'cssPlayerSplash';
		aFrame.innerHTML = "Musiker"
		aFrame.show = MUSIKER_SHOWPANEL;
		aFrame.hide = MUSIKER_HIDEPANEL;
		Musiker.modPlayer.StatusPanel.splashPanel = aFrame;
	Musiker.modPlayer.StatusPanel.appendChild(aFrame);

	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlayerTextBox';
		Musiker.modPlayer.StatusPanel.songTitlePanel = aFrame;
	Musiker.modPlayer.StatusPanel.appendChild(aFrame);

	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlayerTextBox';
		Musiker.modPlayer.StatusPanel.songArtistPanel = aFrame;
	Musiker.modPlayer.StatusPanel.appendChild(aFrame);

	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlayerProgressFrame';
		aFrame.show = MUSIKER_SHOWPANEL;
		aFrame.hide = MUSIKER_HIDEPANEL;
		aFrame.onclick = MUSIKER_PLAYER_SCRUBCLICK;
		Musiker.modPlayer.StatusPanel.ProgressFrame = aFrame;
	Musiker.modPlayer.StatusPanel.appendChild(aFrame);

		var aFrame = document.createElement('span');
			aFrame.className = 'cssPlayerTimeBox';
			aFrame.innerHTML = '00:00';
			aFrame.setAttribute('a',true);
			Musiker.modPlayer.StatusPanel.ProgressFrame.timeElapsed = aFrame;
		Musiker.modPlayer.StatusPanel.ProgressFrame.appendChild(aFrame);
	
		var aFrame = document.createElement('span');
			aFrame.className = 'cssPlayerTimeBox';
			aFrame.innerHTML = '00:00';
			aFrame.setAttribute('b',true);
			Musiker.modPlayer.StatusPanel.ProgressFrame.timeRemaining = aFrame;
		Musiker.modPlayer.StatusPanel.ProgressFrame.appendChild(aFrame);

		var aFrame = document.createElement('div');
			aFrame.className = 'cssPlayerLoadFill';
			Musiker.modPlayer.StatusPanel.ProgressFrame.loadProgress = aFrame;
		Musiker.modPlayer.StatusPanel.ProgressFrame.appendChild(aFrame);
	
		var aFrame = document.createElement('div');
			aFrame.className = 'cssPlayerTimeFill';
			Musiker.modPlayer.StatusPanel.ProgressFrame.timeProgress = aFrame;
		Musiker.modPlayer.StatusPanel.ProgressFrame.appendChild(aFrame);



	//PLAYBACK CONTROL BUTTONS FRAME
	var aFrame = document.createElement('div');
		aFrame.className = 'cssPlayerControlFrame';
		Musiker.modPlayer.ControlsPanel = aFrame;
	Musiker.frames.HeaderPanel.appendChild(aFrame);

		var aFrame = document.createElement('div');
			aFrame.className = 'cssPlayerControlButton_Small';
			aFrame.innerHTML = "<img src='images/backIcon.png'>"
			Musiker.modPlayer.ControlsPanel.buttonPrevious = aFrame;
			aFrame.onmousedown = MUSIKER_PLAYERBUTTONMOUSEDOWN;
			aFrame.onmouseup = MUSIKER_PLAYERBUTTONMOUSEUP;
			aFrame.onclick = function () {MediaPlayer.back();}
		Musiker.modPlayer.ControlsPanel.appendChild(aFrame);

		var aFrame = document.createElement('div');
			aFrame.className = 'cssPlayerControlButton_Large';
			aFrame.innerHTML = Musiker.PLAYICONHTML;
			Musiker.modPlayer.ControlsPanel.buttonPlayPause = aFrame;
			aFrame.onmousedown = MUSIKER_PLAYERBUTTONMOUSEDOWN;
			aFrame.onmouseup = MUSIKER_PLAYERBUTTONMOUSEUP;
			aFrame.onclick = function () {MediaPlayer.togglePlayback();}
		Musiker.modPlayer.ControlsPanel.appendChild(aFrame);

		var aFrame = document.createElement('div');
			aFrame.className = 'cssPlayerControlButton_Small';
			aFrame.innerHTML = "<img src='images/forwardIcon.png'>"
			Musiker.modPlayer.ControlsPanel.buttonNext = aFrame;
			aFrame.onmousedown = MUSIKER_PLAYERBUTTONMOUSEDOWN;
			aFrame.onmouseup = MUSIKER_PLAYERBUTTONMOUSEUP;
			aFrame.onclick = function () {MediaPlayer.nextTrack();}
		Musiker.modPlayer.ControlsPanel.appendChild(aFrame);


	//VOLUME CONTROL
	var aFrame = document.createElement('table');
		aFrame.className = 'cssVolumeControlContainer';
		aFrame.setAttribute('cols',10);
		aFrame.vblocks = Array();
		aFrame.updateVolume = function () {
			for(var i=0;i<Musiker.modPlayer.VolumeBar.vblocks.length;i++) {
				var bF = Musiker.modPlayer.VolumeBar.vblocks[i];
				if (MediaPlayer.currentVolume >= bF.vLevel) bF.setAttribute('filled','true');
				else bF.removeAttribute('filled');
			}
		}
		Musiker.modPlayer.VolumeBar = aFrame;
	Musiker.frames.HeaderPanel.appendChild(aFrame);
	
		var	bFrame = document.createElement('tr');
		for(var i=0;i<10;i++) {
			var tFrame = document.createElement('td');
				tFrame.onclick = MUSIKER_PLAYER_VOLUME_CLICK;
				tFrame.vLevel = (i+1)*10;
			Musiker.modPlayer.VolumeBar.vblocks[i] = tFrame;
			bFrame.appendChild(tFrame);
		}
		Musiker.modPlayer.VolumeBar.appendChild(bFrame);


	//PLAYER WIDGET CONTAINER
	var aFrame = document.createElement('div');
		aFrame.className = 'cssFlashPlayerContainer';
		aFrame.setAttribute("id","flashPlayerContainer");
		Musiker.modPlayer.FlashPanel = aFrame;
	Musiker.frames.HeaderPanel.appendChild(aFrame);
	MediaPlayer.build('flashPlayerContainer');
	
	
	
	
	Musiker.modPlayer.clearStatus();
}

Musiker.modPlayer.clearStatus = function () {
	Musiker.modPlayer.StatusPanel.ProgressFrame.loadProgress.style.width = 0;
	Musiker.modPlayer.StatusPanel.ProgressFrame.timeProgress.style.width = 0;
	Musiker.modPlayer.StatusPanel.ProgressFrame.timeElapsed.innerHTML = "";
	Musiker.modPlayer.StatusPanel.ProgressFrame.timeRemaining.innerHTML = "";
	Musiker.modPlayer.StatusPanel.songTitlePanel.innerHTML = "";
	Musiker.modPlayer.StatusPanel.songArtistPanel.innerHTML = "";
	Musiker.modPlayer.ControlsPanel.buttonPlayPause.innerHTML = Musiker.PLAYICONHTML;
	
	Musiker.modPlayer.StatusPanel.ProgressFrame.hide();
	Musiker.modPlayer.StatusPanel.splashPanel.show();
}

MUSIKER_PLAYER_SCRUBCLICK = function (e) {
	if (window.event)	var perc = window.event.offsetX / this.clientWidth; //IE Event model
	if (e.layerX)	var perc = e.layerX / this.clientWidth; //FF Event Model
	
	var scrubTime = MediaPlayer.currentLength * perc;

	MediaPlayer.setPosition(scrubTime);
}

MUSIKER_PLAYERBUTTONMOUSEDOWN = function () {
	this.setAttribute('down',true);
	return false;
}

MUSIKER_PLAYERBUTTONMOUSEUP = function () {
	this.removeAttribute('down');
}

MUSIKER_PLAYER_VOLUME_CLICK = function () {
	MediaPlayer.setVolume(this.vLevel);
}


// player status update events-----------------------------------------------------------------------------------

MediaPlayer.eventLoadChange = function () {
	Musiker.modPlayer.StatusPanel.ProgressFrame.loadProgress.style.width = MediaPlayer.currentLoad+"%";
	if (MediaPlayer.currentLoad<100) Musiker.modPlayer.StatusPanel.ProgressFrame.timeRemaining.innerHTML = MediaPlayer.currentLoad+"%";

	//player data doesn't always update on track change, so we check it with the load update.
	if (MediaPlayer.currentSong().title != Musiker.modPlayer.StatusPanel.songTitlePanel.textContent) MediaPlayer.eventStateChange();
}
MediaPlayer.eventUpdateTime = function () {
	Musiker.modPlayer.StatusPanel.ProgressFrame.timeElapsed.innerHTML = ConvertSeconds(MediaPlayer.currentPosition);

	if (MediaPlayer.currentLoad>=100) Musiker.modPlayer.StatusPanel.ProgressFrame.timeRemaining.innerHTML = "-"+ConvertSeconds(MediaPlayer.currentRemaining);

	Musiker.modPlayer.StatusPanel.ProgressFrame.timeProgress.style.width = (MediaPlayer.currentPosition/(MediaPlayer.currentSong().duration)*100)+"%";
}
MediaPlayer.eventStateChange = function () {
	var song = MediaPlayer.currentSong();
	if (song['title']) Musiker.modPlayer.StatusPanel.songTitlePanel.innerHTML = song['title'];
	if (song['author']) Musiker.modPlayer.StatusPanel.songArtistPanel.innerHTML = song['author'];
	if (MediaPlayer.playing()) Musiker.modPlayer.ControlsPanel.buttonPlayPause.innerHTML = Musiker.PAUSEICONHTML;
	else Musiker.modPlayer.ControlsPanel.buttonPlayPause.innerHTML = Musiker.PLAYICONHTML;
	
	if (MediaPlayer.currentState>0) {
		Musiker.modPlayer.StatusPanel.ProgressFrame.show();
		Musiker.modPlayer.StatusPanel.splashPanel.hide();
	}
	
	if (Musiker.modNowPlaying) setTimeout("Musiker.modNowPlaying.update();",100);
}
MediaPlayer.eventVolumeChange = function () {
	Musiker.modPlayer.VolumeBar.updateVolume();
}

ConvertSeconds = function (total) {
	var minutes = Math.floor(total/60);
	var seconds = total % 60;
	return minutes + ":" + seconds.toPaddedString(2)
}

Musiker.RegisterModule(Musiker.modPlayer);
